# -*- coding:utf8 -*-
import requests,json
from pyquery import PyQuery as pq

headers = {
    'Accept':'application/json',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8',
    'Connection': 'keep-alive',
    'Content-Length': '6',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie':'UM_distinctid=169e5d3f319232-0888822feaffcf-1a201708-1fa400-169e5d3f31a696;f8c3b_winduser=AQMCU1YGVz4EVl8DWwUKAloDAgYGUlMDXA9QUQBWAQdWBFtSAwADV2o; \
    f8c3b_ck_info=%2F%09tiboo.cn; f8c3b_lastpos=other; f8c3b_lastvisit=368%091554340608%09%2Fm.php%3Fqjy%26aajax%26bindex%26nowtime1554340608178',
    'Host': 'm.tiboo.cn',
    'Origin': 'https://m.tiboo.cn',
    'Referer': 'https://m.tiboo.cn/m.php?q=jy',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
}
session = requests.Session()
def start_loop():
    page = '1'
    content = download_page(page)
    #content = search(page)
    analyse(content)

def download_page(page):
    url = "https://m.tiboo.cn/m.php?q=jy&a=ajax&b=list"
    data={'page':page}
    res = session.post(url,data=data,headers=headers,verify=False)
    cont = res.content
    cont = json.loads(cont)
    cont =  cont['html2']
    return cont

def login():
    url_login = 'https://m.tiboo.cn/m.php?q=jy&a=reg&b=mobile'
    headers['Referer'] = 'https://m.tiboo.cn/m.php?q=jy&a=reg'
    data = {'mobile':'15652515134'}
    session.post(url_login,data=data,headers=headers,verify=False)
    return session

def search(page):
    url = 'https://m.tiboo.cn/m.php?q=jy&a=ajax&b=select'
    data = {
        'edg': '0-0', #年龄
        'tool': '0-0', #身高
        'shouru': '0',
        'xueli': '4',   #学历，0不限，1中专高中，2大专，3本科，4硕士,5小学初中
        'hyin': '0',
        'menu': '0',
        'searchnum': '0',   #
        'tid': '2', #性别，1男2女
        'page': page #页号
    }
    res = session.post(url, data=data, headers=headers, verify=False)
    cont = res.content
    cont = json.loads(cont)
    print cont['html2']

def analyse(html):
    print html
    doc_html = pq(html)
    for li in list(doc_html('li').items())[-2:-1]:
        info =  li('a').attr['href']
        detail_url = info[info.index("'")+1:info.rindex("'")]
        fields = li.text().split('\n')
        likes = fields[0]
        name = fields[1]
        age = fields[2]
        height = fields[4]
        xueli = fields[6]
        earning = fields[8]
        if len(fields)>=10:
            remark = fields[9]
        else:
            remark = ''
        list_yz = []
        for yanzheng in  li('em[class]').items():
            list_yz.append(yanzheng.attr['class'])
        print list_yz
        print detail_url,li('img').attr['src']
        print likes,name,age,height,xueli,earning,remark
        break

if __name__=='__main__':
    start_loop()